package cn.wuxing.mediation.businessplatform.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.wuxing.mediation.businessplatform.domain.annotation.BusinessTag;
import cn.wuxing.mediation.businessplatform.domain.constants.BusinessListConstants;
import cn.wuxing.mediation.businessplatform.domain.dto.PreLitigationMediationEventListDTO;
import cn.wuxing.mediation.businessplatform.domain.enums.EventCategoryEnum;
import cn.wuxing.mediation.businessplatform.domain.enums.EventStateEnum;
import cn.wuxing.mediation.businessplatform.domain.vo.ChangeWindowDetailVO;
import cn.wuxing.mediation.businessplatform.domain.vo.OrdinaryDisputeEventLisVO;
import cn.wuxing.mediation.businessplatform.domain.vo.PreLitigationMediationEventListVO;
import cn.wuxing.mediation.businessplatform.factory.BusinessFactory;
import cn.wuxing.mediation.businessplatform.mapper.business.CmEventApplicantMapper;
import cn.wuxing.mediation.businessplatform.mapper.system.DictDataMapper;
import cn.wuxing.mediation.businessplatform.service.BaseBusinessListService;
import cn.wuxing.mediation.common.core.domain.entity.CmEventApplicant;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: <描述>
 * @date: 2024/9/15 21:07
 * @author: yang
 * @version: 1.0
 */
@Service
@BusinessTag(tag = BusinessListConstants.PRE_LITIGATION_MEDIATION_EVENT)
public class PreLitigationMediationEventListServiceImpl extends
        BaseBusinessListServiceImpl<PreLitigationMediationEventListVO, PreLitigationMediationEventListDTO> {

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private CmEventApplicantMapper cmEventApplicantMapper;

    private static final String DIVISION_GROUP = "divisionGroup";
    private static final String NOTARY_MEDIATE_OFFICE = "notaryMediateOffice";
    @Override
    public ChangeWindowDetailVO changeWindowDetail(Long eventId) {
        return getChangeWindowDetail(eventId, EventCategoryEnum.PRE_MEDIATION_EVENT.getCode());
    }


    @Override
    public Map<String, Integer> getBusinessLgetApplicantHistoryNum(String idCard) {
        Integer num = this.cmEventApplicantMapper.selectCount(
                Wrappers.<CmEventApplicant>lambdaQuery().eq(CmEventApplicant::getIdCard, idCard)
                        .eq(CmEventApplicant::getEventCategory, EventCategoryEnum.PRE_MEDIATION_EVENT));
        Map<String, Integer> map = new HashMap<>(1);
        map.put("num", num == null ? 0 : num);
        return map;
    }


    @Override
    public void importTemplate(HttpServletRequest request, HttpServletResponse response) {
        List<PreLitigationMediationEventListVO> list = BusinessFactory.doInstanceRealization(BusinessListConstants.PRE_LITIGATION_MEDIATION_EVENT,
                        BaseBusinessListService.class)
                .getBusinessList(request);
        ExcelUtil<PreLitigationMediationEventListVO> util = new ExcelUtil<PreLitigationMediationEventListVO>(
                PreLitigationMediationEventListVO.class);
        util.exportExcel(response, list,  "诉前调解件");
    }

    @Override
    void handlerList(List<PreLitigationMediationEventListVO> list) {
        if (CollUtil.isEmpty(list)) {
            return;
        }

        list.forEach(item -> {
            String dictLabel = this.dictDataMapper.selectDictLabel("bus_dispute_source", item.getEventSource());
            item.setEventSource(dictLabel);
            item.setEventStatus(EventStateEnum.getDescEn(item.getEventState()));
            item.setMediationResult("1".equals(item.getMediationResult())? "调解成功" : "调解失败");
        });

    }

    @Override
    void handerParam(PreLitigationMediationEventListDTO dto) {
        if (StringUtils.isNotBlank(dto.getPageType())) {
            switch (dto.getPageType()) {
                // 列表类型 1.待受理 2.已受理 3.待调解 4.已调解
                case "1":
                    dto.setEventStates(Arrays.asList(EventStateEnum.WAIT_ACCEPT.getCode(),
                            EventStateEnum.WAIT_SECOND_ACCEPT.getCode()));
                    break;

                case "2":
                    dto.setEventStates(
                            Arrays.asList(EventStateEnum.WAIT_MEDIATE.getCode(),
                                    EventStateEnum.WAIT_SECOND_MEDIATE.getCode()));
                    break;

                case "3":
                    dto.setEventStates(Arrays.asList(EventStateEnum.WAIT_MEDIATE.getCode(),
                            EventStateEnum.WAIT_SECOND_MEDIATE.getCode()));
                    break;

                case "4":
                    dto.setEventStates(Arrays.asList(EventStateEnum.WAIT_CLOSE.getCode(),
                            EventStateEnum.WAIT_SECOND_CLOSE.getCode(), EventStateEnum.CLOSE.getCode()));
                    break;

            }
        }

        // 分案小组可以看所有窗口的数据
        List<String> divisionGroupTypes = Arrays.asList("1", "2", "3", "4");
        // 如果是专窗受理列表则分案小组可见
        if (divisionGroupTypes.contains(dto.getPageType())) {
            if (SecurityUtils.hasRole(DIVISION_GROUP)) {
                dto.setIsDivisionGroup(1);
            }
        }
        List<String> types = Arrays.asList("3", "4");
        // 如果是统一调解列表则公正调解室可见待二次调解的数据
        if (types.contains(dto.getPageType())) {
            if (SecurityUtils.hasRole(NOTARY_MEDIATE_OFFICE)) {
                dto.setIsNotaryMediateOffice(1);
            }
        }
    }
}
